# 计算日期周期比
import datetime

date_sale = {}
is_first_line = True
with open('027.txt', "r", encoding="utf-8") as file:
    for line in file:
        # print(line)
        if is_first_line:
            is_first_line = False
            continue
        # line = line[:-1]
        line = line.strip()
        date, sale_number = line.split("\t")
        date_sale[date] = float(sale_number)
        # print(date, sale_number)
print(date_sale)


def get_diff_days(date, days):
    # 将日期字符串转为日期对象
    curr_date = datetime.datetime.strptime(date, "%Y-%m-%d")
    timedelta = datetime.timedelta(days=-days)
    # 将日期对象转为字符串
    target_date = (curr_date + timedelta).strftime("%Y-%m-%d")
    return target_date


for date, sale_number in date_sale.items():
    date7 = get_diff_days(date, 7)
    sale_number7 = date_sale.get(date7, 0)
    # print(sale_number7)
    if sale_number7 == 0:
        print(date, sale_number, 0)
    else:
        week_diff = (sale_number - sale_number7) / sale_number7
        print(date, sale_number, date7, sale_number7, week_diff)
